<template>
  <div class="profile-container">
    <div class="profile-header">
      <h1>个人信息</h1>
      <p>查看和管理您的个人信息</p>
    </div>

    <div class="profile-card" v-if="user">
      <div class="profile-avatar">
        <div class="avatar-placeholder">
          {{ user.username.charAt(0).toUpperCase() }}
        </div>
        <h2>{{ user.username }}</h2>
        <p class="user-role">{{ userRole }}</p>
      </div>

      <div class="profile-info">
        <div class="info-section">
          <h3>基本信息</h3>
          <div class="info-grid">
            <div class="info-item">
              <label>用户名:</label>
              <span>{{ user.username }}</span>
            </div>
            <div class="info-item">
              <label>邮箱:</label>
              <span>{{ user.email }}</span>
            </div>
            <div class="info-item" v-if="user.realName">
              <label>真实姓名:</label>
              <span>{{ user.realName }}</span>
            </div>
            <div class="info-item" v-if="user.phone">
              <label>手机号:</label>
              <span>{{ user.phone }}</span>
            </div>

            </div>
          </div>
        </div>

        <div class="info-section">
          <h3>账户信息</h3>
          <div class="info-grid">
            <div class="info-item">
              <label>用户状态:活跃</label>
             
            </div>

            </div>
          </div>
        </div>
      </div>

      <div class="profile-actions">
        <button class="btn-edit" @click="editProfile">编辑信息</button>
        <button class="btn-change-password" @click="changePassword">修改密码</button>
      </div>


    <!-- 修改密码模态框 -->
    <div v-if="showPasswordModal" class="modal-overlay" @click="showPasswordModal = false">
      <div class="modal-content" @click.stop>
        <h3>修改密码</h3>
        <form @submit.prevent="handlePasswordChange" class="password-form">
          <div class="form-group">
            <label>当前密码</label>
            <input
              type="password"
              v-model="passwordForm.currentPassword"
              required
              placeholder="请输入当前密码"
            />
          </div>
          <div class="form-group">
            <label>新密码</label>
            <input
              type="password"
              v-model="passwordForm.newPassword"
              required
              placeholder="请输入新密码"
            />
          </div>
          <div class="form-group">
            <label>确认新密码</label>
            <input
              type="password"
              v-model="passwordForm.confirmPassword"
              required
              placeholder="请再次输入新密码"
            />
          </div>
          <div class="modal-actions">
            <button type="button" class="btn-cancel" @click="showPasswordModal = false">取消</button>
            <button type="submit" class="btn-confirm">确认修改</button>
          </div>
        </form>
      </div>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import type { User } from '@/api/user'

const router = useRouter()
const userStore = useUserStore()

const user = ref<User | null>(null)
const showPasswordModal = ref(false)

const passwordForm = reactive({
  currentPassword: '',
  newPassword: '',
  confirmPassword: ''
})

const userRole = computed(() => {
  return userStore.userRole === 'admin' ? '管理员' : '普通用户'
})

const formatDate = (dateString?: string) => {
  if (!dateString) return '-'
  return new Date(dateString).toLocaleDateString('zh-CN')
}

const formatDateTime = (dateString?: string) => {
  if (!dateString) return '-'
  return new Date(dateString).toLocaleString('zh-CN')
}

const editProfile = () => {
  if (user.value?.userId) {
    router.push(`/admin/users/edit/${user.value.userId}`)
  }
}

const changePassword = () => {
  showPasswordModal.value = true
}

const handlePasswordChange = () => {
  // 这里实现修改密码的逻辑
  alert('修改密码功能正在开发中...')
  showPasswordModal.value = false
  // 清空表单
  Object.assign(passwordForm, {
    currentPassword: '',
    newPassword: '',
    confirmPassword: ''
  })
}

const loadUser = async () => {
  const userInfo = localStorage.getItem('userInfo')
  if (userInfo) {
    user.value = JSON.parse(userInfo)
  } else {
    const userData = await userStore.fetchCurrentUser()
    if (userData) {
      user.value = userData
    }
  }
}

onMounted(() => {
  loadUser()
})
</script>

<style scoped>
.profile-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 40px 20px;
}

.profile-header {
  text-align: center;
  margin-bottom: 40px;
}

.profile-header h1 {
  font-size: 32px;
  color: #1e293b;
  margin-bottom: 8px;
  font-weight: 600;
}

.profile-header p {
  color: #64748b;
  font-size: 16px;
}

.profile-card {
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
  padding: 40px;
  border: 1px solid #e2e8f0;
}

.profile-avatar {
  text-align: center;
  margin-bottom: 40px;
  padding-bottom: 40px;
  border-bottom: 1px solid #f1f5f9;
}

.avatar-placeholder {
  width: 80px;
  height: 80px;
  background: linear-gradient(135deg, #3b82f6, #1d4ed8);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 32px;
  font-weight: bold;
  margin: 0 auto 16px;
}

.profile-avatar h2 {
  font-size: 24px;
  color: #1e293b;
  margin-bottom: 8px;
}

.user-role {
  color: #64748b;
  font-size: 14px;
  background: #f1f5f9;
  padding: 4px 12px;
  border-radius: 20px;
  display: inline-block;
}

.profile-info {
  margin-bottom: 40px;
}

.info-section {
  margin-bottom: 32px;
}

.info-section:last-child {
  margin-bottom: 0;
}

.info-section h3 {
  font-size: 18px;
  color: #1e293b;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 2px solid #f1f5f9;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
}

.info-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f8fafc;
}

.info-item:last-child {
  border-bottom: none;
}

.info-item label {
  font-weight: 500;
  color: #374151;
  font-size: 14px;
}

.info-item span {
  color: #64748b;
  text-align: right;
}

.status-active {
  color: #10b981;
  font-weight: 500;
}

.status-inactive {
  color: #ef4444;
  font-weight: 500;
}

.profile-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  padding-top: 32px;
  border-top: 1px solid #f1f5f9;
}

.btn-edit {
  background: #3b82f6;
  color: white;
  border: none;
  padding: 12px 24px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 500;
  transition: background-color 0.2s;
}

.btn-edit:hover {
  background: #2563eb;
}

.btn-change-password {
  background: #f1f5f9;
  color: #374151;
  border: 1px solid #e2e8f0;
  padding: 12px 24px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.2s;
}

.btn-change-password:hover {
  background: #e2e8f0;
}

.loading {
  text-align: center;
  padding: 60px 20px;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f1f5f9;
  border-top: 4px solid #3b82f6;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading p {
  color: #64748b;
}

/* 模态框样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
  max-width: 400px;
  width: 90%;
}

.modal-content h3 {
  margin-bottom: 20px;
  color: #1e293b;
  text-align: center;
}

.password-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.password-form .form-group {
  display: flex;
  flex-direction: column;
}

.password-form label {
  color: #374151;
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 6px;
}

.password-form input {
  padding: 12px 16px;
  border: 2px solid #e5e7eb;
  border-radius: 8px;
  font-size: 14px;
  transition: all 0.2s;
}

.password-form input:focus {
  outline: none;
  border-color: #3b82f6;
}

.modal-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  margin-top: 20px;
}

.btn-cancel {
  background: #6b7280;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 6px;
  cursor: pointer;
}

.btn-cancel:hover {
  background: #4b5563;
}

.btn-confirm {
  background: #3b82f6;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 6px;
  cursor: pointer;
}

.btn-confirm:hover {
  background: #2563eb;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .profile-card {
    padding: 30px 20px;
  }
  
  .info-grid {
    grid-template-columns: 1fr;
  }
  
  .profile-actions {
    flex-direction: column;
  }
  
  .info-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
  
  .info-item span {
    text-align: left;
  }
}
</style>